package com.fzt.admin.remote.dto;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fzt.admin.common.convention.result.Result;
import com.fzt.admin.remote.dto.req.ShortLinkCreateReqDTO;
import com.fzt.admin.remote.dto.req.ShortLinkPageReqDTO;
import com.fzt.admin.remote.dto.req.ShortLinkUpdateReqDTO;
import com.fzt.admin.remote.dto.resp.ShortLinkCreateRespDTO;
import com.fzt.admin.remote.dto.resp.ShortLinkGroupCountQueryRespDTO;
import com.fzt.admin.remote.dto.resp.ShortLinkPageRespDTO;

import java.util.HashMap;
import java.util.List;

/**
 * 短链接中台远程调用
 */
public interface ShortLinkRemoteService {

    /**
     * 远程调用分页查询短链接
     * @param reqDTO
     * @return
     */
    default Result<IPage<ShortLinkPageRespDTO>> pageShortLink(ShortLinkPageReqDTO reqDTO){
        HashMap<String, Object> requestMap = new HashMap<>();
        requestMap.put("gid",reqDTO.getGid());
        requestMap.put("current", reqDTO.getCurrent());
        requestMap.put("size", reqDTO.getSize());
        String resultPageStr = HttpUtil.get("http://127.0.0.1:8001/api/short-link/v1/page", requestMap);
        return JSON.parseObject(resultPageStr, new TypeReference<>() {
        });
    }

    /**
     * 远程调用创建短链接
     * @param reqDTO
     * @return
     */
    default Result<ShortLinkCreateRespDTO> createShortLink(ShortLinkCreateReqDTO reqDTO){
        String resultBodyStr = HttpUtil.post("http://127.0.0.1:8001/api/short-link/v1/create", JSON.toJSONString(reqDTO));
        return JSON.parseObject(resultBodyStr, new TypeReference<>() {
        });
    }

    /**
     * 查询分组短链接总量
     */
    default Result<List<ShortLinkGroupCountQueryRespDTO>> listGroupShortLinkCount(List<String> reqDTO){
        HashMap<String, Object> requestMap = new HashMap<>();
        requestMap.put("reqDTO",reqDTO);
        String resultStr = HttpUtil.get("http://127.0.0.1:8001/api/short-link/v1/count", requestMap);
        return JSON.parseObject(resultStr, new TypeReference<>() {
        });
    }

    /**
     * 修改短链接
     * @param reqParam 请求参数
     */
    default void updateShortLink(ShortLinkUpdateReqDTO reqParam){
        String resultStr = HttpUtil.post("http://127.0.0.1:8001/api/short-link/v1/update", JSON.toJSONString(reqParam));
    }
}
